package com.sxm.yiwei.fragments;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.sxm.yiwei.R;
import com.sxm.yiwei.constant.Common;
import com.sxm.yiwei.constant.Constant;
import com.sxm.yiwei.pinche.PincheOrderActivity;


/**
 * A simple {@link Fragment} subclass.
 * 我是车主
 */
public class CarOwnerFragment extends BaseFragment implements View.OnClickListener{
    private RelativeLayout rl_wait_pay;
    private RelativeLayout rl_wait_take;
    private RelativeLayout rl_wait_service;
    private RelativeLayout rl_in_service;
    private RelativeLayout rl_wait_finish;
    private RelativeLayout rl_finished;
    private RelativeLayout rl_loss;
    private RelativeLayout rl_exception;
    private TextView tv_wait_pay_num;
    private TextView tv_wait_take_num;
    private TextView tv_wait_service_num;
    private TextView tv_in_service_num;
    private TextView tv_wait_finish_num;
    private TextView tv_finished_num;
    private TextView tv_loss_num;
    private TextView tv_exception_num;

    public CarOwnerFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_shop_car_owner, container, false);
        initView(view);
        return view;
    }

    @Override
    public void onResume() {
        getOrderNum();
        super.onResume();
    }

    private void initView(View view) {
        rl_wait_pay = (RelativeLayout) view.findViewById(R.id.rl_wait_pay);
        rl_wait_take = (RelativeLayout) view.findViewById(R.id.rl_wait_take);
        rl_wait_service = (RelativeLayout) view.findViewById(R.id.rl_wait_service);
        rl_in_service = (RelativeLayout) view.findViewById(R.id.rl_in_service);
        rl_wait_finish = (RelativeLayout) view.findViewById(R.id.rl_wait_finish);
        rl_finished = (RelativeLayout)view.findViewById(R.id.rl_finished);
        rl_loss = (RelativeLayout)view.findViewById(R.id.rl_loss);
        rl_exception = (RelativeLayout)view.findViewById(R.id.rl_exception);

        tv_wait_pay_num = (TextView)view.findViewById(R.id.tv_wait_pay_num);
        tv_wait_take_num = (TextView)view.findViewById(R.id.tv_wait_take_num);
        tv_wait_service_num = (TextView)view.findViewById(R.id.tv_wait_service_num);
        tv_in_service_num = (TextView)view.findViewById(R.id.tv_in_service_num);
        tv_wait_finish_num = (TextView)view.findViewById(R.id.tv_wait_finish_num);
        tv_finished_num = (TextView)view.findViewById(R.id.tv_finished_num);
        tv_loss_num = (TextView)view.findViewById(R.id.tv_loss_num);
        tv_exception_num = (TextView)view.findViewById(R.id.tv_exception_num);

        rl_wait_pay.setOnClickListener(this);
        rl_wait_take.setOnClickListener(this);
        rl_wait_service.setOnClickListener(this);
        rl_in_service.setOnClickListener(this);
        rl_wait_finish.setOnClickListener(this);
        rl_finished.setOnClickListener(this);
        rl_loss.setOnClickListener(this);
        rl_exception.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.rl_wait_pay:
                Intent intentWP = new Intent(getActivity(), PincheOrderActivity.class);
                intentWP.putExtra("identity", Constant.HOST);
                intentWP.putExtra("order_type",Constant.WAIT_PAY);
                startActivity(intentWP);
                break;
            case R.id.rl_wait_take:
                Intent intentWT = new Intent(getActivity(), PincheOrderActivity.class);
                intentWT.putExtra("identity", Constant.HOST);
                intentWT.putExtra("order_type",Constant.WAIT_TAKE);
                startActivity(intentWT);
                break;
            case R.id.rl_wait_service:
                Intent intentWS = new Intent(getActivity(), PincheOrderActivity.class);
                intentWS.putExtra("identity", Constant.HOST);
                intentWS.putExtra("order_type",Constant.WAIT_SURVICE);
                startActivity(intentWS);
                break;
            case R.id.rl_in_service:
                Intent intentIS = new Intent(getActivity(), PincheOrderActivity.class);
                intentIS.putExtra("identity", Constant.HOST);
                intentIS.putExtra("order_type",Constant.IN_SURVICE);
                startActivity(intentIS);
                break;
            case R.id.rl_wait_finish:
                Intent intentWF = new Intent(getActivity(), PincheOrderActivity.class);
                intentWF.putExtra("identity", Constant.HOST);
                intentWF.putExtra("order_type",Constant.WAIT_FINISH);
                startActivity(intentWF);
                break;
            case R.id.rl_finished:
                Intent intentF = new Intent(getActivity(), PincheOrderActivity.class);
                intentF.putExtra("identity", Constant.HOST);
                intentF.putExtra("order_type",Constant.FINISHED);
                startActivity(intentF);
                break;
            case R.id.rl_loss:
                Intent intentL = new Intent(getActivity(), PincheOrderActivity.class);
                intentL.putExtra("identity", Constant.HOST);
                intentL.putExtra("order_type",Constant.LOSS);
                startActivity(intentL);
                break;
            case R.id.rl_exception:
                Intent intentE = new Intent(getActivity(), PincheOrderActivity.class);
                intentE.putExtra("identity", Constant.HOST);
                intentE.putExtra("order_type",Constant.EXCEPTION);
                startActivity(intentE);
                break;

        }
    }

    /*
    * 获取每种订单的数量，赋值给控件
    * */
    private void getOrderNum(){
        JSONObject object = new JSONObject();
        object.put("role",Constant.HOST+"");
        httpPost(getActivity(), Common.HOST+Common.ORDER_NUM,object,"车主订单数量");
    }

    @Override
    public void onSuccess(String result, String fromWhere) {
        super.onSuccess(result, fromWhere);
        com.alibaba.fastjson.JSONObject obj = JSON.parseObject(result);
        JSONObject data = obj.getJSONObject("data");
        if (fromWhere.equals("车主订单数量")){
            tv_wait_pay_num.setText(data.getIntValue("noPayNum")+"");
            tv_wait_take_num.setText(data.getIntValue("waitTakeOrderNum")+"");
            tv_wait_service_num.setText(data.getIntValue("waitServiceNum")+"");
            tv_in_service_num.setText(data.getIntValue("servicingNum")+"");
            tv_wait_finish_num.setText(data.getIntValue("waitEndNum")+"");
            tv_finished_num.setText(data.getIntValue("completeNum")+"");
            tv_loss_num.setText(data.getIntValue("loseNum")+"");
            tv_exception_num.setText(data.getIntValue("rtgNum")+"");
        }

    }
}
